package com.example.myapplicationndk.aidl;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplicationndk.R;

import java.util.List;

public class MainAidlActivity extends AppCompatActivity {

    private String TAG = this.getClass().getSimpleName();

    private IBookManager mRemoteBookManager;

    private ServiceConnection mConnection = new ServiceConnection() {

        // ，onServiceDisconnected运行在main线程中
        public void onServiceConnected(ComponentName className, IBinder service) {
            // IBookManager.Stub 表示
            IBookManager bookManager = IBookManager.Stub.asInterface(service);
            mRemoteBookManager = bookManager;
            try {
                mRemoteBookManager.asBinder().linkToDeath(mDeathRecipient, 0);
                List<Book> list = bookManager.getBookList();
                Log.d(TAG, "query book list, list type:"
                        + list.getClass().getCanonicalName());
                Log.d(TAG, "query book list:" + list.toString());
                Book newBook = new Book(3, "Android进阶");
                mRemoteBookManager.addBook(newBook);
                Log.d(TAG, "add book:" + newBook);
                List<Book> newList = bookManager.getBookList();
                Log.d(TAG, "query book list:" + newList.toString());
                mRemoteBookManager.registerListener(mOnNewBookArrivedListener);
            } catch (RemoteException e) {
                e.printStackTrace();
                Log.d("eee", e.toString());
            }
        }

        public void onServiceDisconnected(ComponentName className) {

        }
    };

    private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {
        @Override
        public void binderDied() {
            Log.d(TAG, "binder died. tname:" + Thread.currentThread().getName());
            if (mRemoteBookManager == null)
                return;
            mRemoteBookManager.asBinder().unlinkToDeath(mDeathRecipient, 0);
            mRemoteBookManager = null;
            // TODO:这里重新绑定远程Service
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_aidl);

        Intent intent = new Intent(this, MyService.class);
        boolean i = bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        Log.d(TAG, i + "");
    }

    private INewBookArrivedListener mOnNewBookArrivedListener = new INewBookArrivedListener.Stub() {

        @Override
        public void onNewBookArrived(Book newBook) throws RemoteException {
            Log.d(TAG, newBook.toString());
        }
    };

    @Override
    protected void onDestroy() {
        if (mRemoteBookManager != null
                && mRemoteBookManager.asBinder().isBinderAlive()) {
            try {
                Log.d(TAG, "unregister listener:" + mOnNewBookArrivedListener);
                mRemoteBookManager
                        .unregisterListener(mOnNewBookArrivedListener);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        unbindService(mConnection);
        super.onDestroy();
    }
}